$(function(){
    // 首先为模块命名定义变量
    var datazr = "data-zr"; // 左侧一级菜单的编号
    var datazrlist = "data-zrlist"; // 左侧下拉菜单的第一个UL编号，与父级相同

    function Zrtopmenu(jdom){
        this.jdom = jdom; // 顶部菜单的jquery对象
        this.leftmenu = new Leftmenublock($('div[data-menu-parent='+jdom.attr('data-menu')+']')); // 相对应的左侧列表框
    }

    function Leftmenublock(jdom){
        var _this = this;
        this.jdom = jdom;
        this.listmenu = [];
        jdom.children('div .oe_secondary_menu_section').each(function(){
            _this.listmenu.push(new Menulist($(this)));
        });
        init();
        function changemenu(item,quick,chan){
            return function(){
                item.setSelected(true);
                function chup(){
                    item.setSelected(true);
                    item.setDownde(false,quick); 
                }
                function dow(){
                    _this.listmenu.forEach(function(item2){
                        item2.setSelected(false);
                        item2.downed?item2.setDownde(false,quick):''; 
                    })
                    item.setSelected(true);
                    item.setDownde(true,quick);
                }
                if(chan){
                    dow();
                }else{
                    if(item.downed){ chup();}else{dow();}
                }
                
            }
        }
        function init(){
            _this.listmenu.forEach(function(item){
                item.haschild?item.child.hide():'';
                item.jdom.click(changemenu(item));
                item.changeclick = changemenu(item,true,true);
            })
        }
    }
    Leftmenublock.prototype = {
        constructor:Leftmenublock
    }

    function Menulist(jdom){
        this.jdom = jdom;
        this.haschild = false;
        var child = $('ul['+datazrlist+'='+jdom.attr(datazr)+']');
        this.child = {};
        this.selected = false; // 当前状态是否是选中状态 主要用于左侧蓝条
        this.downed = false; // 当前状态是否是下拉状态 主要用于右侧下拉图标
        if(child.length>0 && child.prev().hasClass('oe_secondary_menu_section')){
            this.haschild = true;
            this.child = child;
        }
    }
    Menulist.prototype = {
        constructor:Menulist,
        setSelected:function(sele){
            this.selected = sele;
            sele?this.jdom.addClass('oe_secondary_menu_section_focus_left'):this.jdom.removeClass('oe_secondary_menu_section_focus_left');
        },
        setDownde:function(dow,quick){
            this.downed = dow;
            dow?this.jdom.addClass('oe_secondary_menu_section_focus'):this.jdom.removeClass('oe_secondary_menu_section_focus');
            if(this.haschild){
                if(quick){
                    dow?this.child.show():this.child.hide();
                }else{
                    dow?this.child.slideDown():this.child.slideUp();
                }
            }
        }
    }

    // 初始化topmenu群
    $('.oe_menu_toggler').each(function(){
        var top_menu = new Zrtopmenu($(this));
        top_menu.jdom.click(function(){
            if( top_menu.leftmenu.listmenu ==undefined || top_menu.leftmenu.listmenu.length<=0 ){
                return;
            }
            var click_func = top_menu.leftmenu.listmenu[0].changeclick;
            if(click_func!=null && click_func!=undefined){
                click_func(top_menu.leftmenu.listmenu[0],true,true);
            }else{
                console.log("changeclick is null!", click_func);
            }
        });
    });


    
    // $("body").on('click','div .oe_secondary_menu_section',function(){
    //     var _this = $(this);
    //     var divid = _this.attr("data-zr");
    //     _this.attr("data-act",parseInt(_this.attr("data-act"))+1);
    //     var isShow = parseInt(_this.attr("data-act"))%2;

    //     // 临时写法（给选中的二级菜单添加样式）
    //     $("div .oe_secondary_menu_section").removeClass('oe_secondary_menu_section_focus');
    //     $("div .oe_secondary_menu_section").removeClass('oe_secondary_menu_section_focus_left');
    //     if(isShow){
    //         // 箭头
    //         $(this).addClass('oe_secondary_menu_section_focus');
    //     }
    //     $(this).addClass('oe_secondary_menu_section_focus_left');
        

    //     $(".oe_secondary_submenu").each(function(){
    //         var menu_parent_id = $(this).attr("data-zrlist");

    //         // // 判断循环中的菜单的上一个元素是否是DIV
    //         var brother = $(this).prev();
    //         if(!$(this).prev().hasClass('oe_secondary_menu_section')){
    //             return;
    //         }

    //         // 调整其他菜单点击时修改已经下拉的菜单的状态
    //         var par = $(".oe_secondary_menu_section[data-zr="+menu_parent_id+"]")
    //         if(menu_parent_id != divid && par.attr("data-act")%2){
    //             par.attr("data-act",parseInt(_this.attr("data-act"))+1);
    //         }

    //         if(menu_parent_id == divid && isShow){
    //             $(this).slideDown();
    //             // 同时可以添加选中的样式
    //         }else{
    //             // 此处可以再优化下不用全部执行，只针对展开的执行
    //             $(this).slideUp();
    //             // _this.removeClass('oe_secondary_menu_section_focus')
    //         }
    //     });
    // });

    // $("div .oe_secondary_menu_section")[0].click();

    // // 上面点击左侧收缩隐藏
    $("body").on('click','.sidebar-toggle',function(){
        $('.o_sub_menu').toggle();
        $('.mylogo').toggle();
    })
})

/** 
 * ir_ui_menu
{
    'web_icon_data': False, 
    'name': u'基础', 
    'web_icon': False, 
    'sequence': 10, 
    'parent_id': (70, u'赏金'), 
    'action': False, 
    'id': 85,
    'children': [
        {
            'web_icon_data': False, 
            'name': u'猎人', 
            'web_icon': False, 
            'sequence': 10, 
            'parent_id': (85, u'赏金/基础'), 
            'action': False, 
            'id': 71,
            'children': [
                {
                    'web_icon_data': False, 
                    'name': u'猎人登记', 
                    'web_icon': False, 
                    'sequence': 10, 
                    'children': [], 
                    'parent_id': (71, u'赏金/基础/猎人'), 
                    'action': 'ir.actions.act_window,77', 
                    'id': 75
                }
            ]
        }, 
        {
            'web_icon_data': False, 
            'name': u'\u4efb\u52a1', 
            'web_icon': False, 
            'sequence': 10, 
            'children': [
                {
                    'web_icon_data': False, 
                    'name': u'\u60ac\u8d4f', 
                    'web_icon': False, 
                    'sequence': 10, 
                    'children': [], 
                    'parent_id': (72, u'\u8d4f\u91d1/\u57fa\u7840/\u4efb\u52a1'), 
                    'action': 'ir.actions.act_window,78', 
                    'id': 76
                }
            ], 
            'parent_id': (85, u'\u8d4f\u91d1/\u57fa\u7840'), 
            'action': False, 
            'id': 72
        }, 
        {
            'web_icon_data': False, 
            'name': u'\u7b49\u7ea7', 
            'web_icon': False, 
            'sequence': 10, 
            'children': [
                {
                    'web_icon_data': False, 
                    'name': u'\u7b49\u7ea7\u6ce8\u518c', 
                    'web_icon': False, 
                    'sequence': 10, 
                    'children': [], 
                    'parent_id': (73, u'\u8d4f\u91d1/\u57fa\u7840/\u7b49\u7ea7'), 
                    'action': 'ir.actions.act_window,79', 
                    'id': 77
                }
            ], 
            'parent_id': (85, u'\u8d4f\u91d1/\u57fa\u7840'), 
            'action': False, 
            'id': 73
        }, 
        {
            'web_icon_data': False, 
            'name': u'\u804c\u9636', 
            'web_icon': False, 
            'sequence': 10, 
            'children': [
                {
                    'web_icon_data': False, 
                    'name': u'\u804c\u9636\u6ce8\u518c', 
                    'web_icon': False, 
                    'sequence': 10, 
                    'children': [], 
                    'parent_id': (74, u'\u8d4f\u91d1/\u57fa\u7840/\u804c\u9636'), 
                    'action': 'ir.actions.act_window,80', 
                    'id': 78
                }
            ], 
            'parent_id': (85, u'\u8d4f\u91d1/\u57fa\u7840'), 
            'action': False, 
            'id': 74
        }
    ], 
    
}

**/